﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;
using System.Data;

namespace SaleManagement.Web.Pages
{
  public partial class PriceList : BasePage
  {
    #region Properties
    private string m_innerError = string.Empty;

    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        LoadCombo();
        LoadPriceList();
      }
    }
    protected void gvPrice_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Save")
      {
        m_innerError = "Save|" + SavePrice();
      }
      LoadPriceList();
    }
    protected void gvPrice_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_innerError;
    }
    protected void gvPrice_PageIndexChanged(object sender, EventArgs e)
    {
      LoadPriceList();
    }

    #endregion

    #region Method
    private void LoadCombo()
    {
      TList<ProductGroup> listProductCategory = new ProductGroupService().GetAll();
      cbxSearchProductType.DataSource = listProductCategory;
      cbxSearchProductType.DataBind();
      cbxProductType.DataSource = listProductCategory;
      cbxProductType.DataBind();
      if (listProductCategory.Count > 0)
      {
        TList<Product> listProduct = new ProductService().GetByGroupId(listProductCategory[0].Id);
        cbxSearchProduct.DataSource = listProduct;
        cbxSearchProduct.DataBind();
        cbxSearchProduct.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem(" ", null));

        cbxProduct.DataSource = listProduct;
        cbxProduct.DataBind();
      }
    }

    protected void cbxSearchProduct_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
      if (cbxSearchProductType.Value != null)
      {
        int productType = Convert.ToInt32(cbxSearchProductType.Value);
        TList<Product> listProduct = new ProductService().GetByGroupId(productType);
        cbxSearchProduct.DataSource = listProduct;
        cbxSearchProduct.DataBind();
        cbxSearchProduct.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem(" ", null));
      }
    }

    protected void cbxProduct_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
      if (cbxProductType.Value != null)
      {
        int productType = Convert.ToInt32(cbxProductType.Value);
        TList<Product> listProduct = new ProductService().GetByGroupId(productType);
        cbxProduct.DataSource = listProduct;
        cbxProduct.DataBind();
      }
    }

    private void LoadPriceList()
    {
      int? productType = null;
      if (cbxSearchProductType.Value != null)
      {
        productType = Convert.ToInt32(cbxSearchProductType.Value);
      }

      int? product = null;
      if (cbxSearchProduct.Value != null)
      {
        product = Convert.ToInt32(cbxSearchProduct.Value);
      }

      gvPrice.DataSource = new Services.PriceService().Search(productType, product);
      gvPrice.DataBind();
    }

    private string SavePrice()
    {
      try
      {
        Price price = new Price();
        price.ProductId = Convert.ToInt32(cbxProduct.Value);
        price.EffectiveDate = DateTime.Now;
        price.Price = Convert.ToInt32(txtPrice.Text);
        price.UpdatedBy = CurrentUser.Username;
        price.UpdatedDate = DateTime.Now;
        new PriceService().Save(price);
        return String.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("PriceList.aspx::Save Price", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
    }

    private string DeletePrice(int priceId)
    {
      return string.Empty;
    }
    #endregion

  }
}
